package com.itboye.jinpuyiyuan.activity.login;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.itboye.jinpuyiyuan.R;
import com.itboye.jinpuyiyuan.base.BaseActivity;
import com.itboye.jinpuyiyuan.presenter.BranchAddressPresenter;
import com.itboye.jinpuyiyuan.utils.sputil.Const;
import com.itboye.jinpuyiyuan.utils.sputil.SPUtils;
import com.itboye.jinpuyiyuan.utils.volley.ResultEntity;
import com.itboye.jinpuyiyuan.widget.loadingutil.MAlert;

import java.util.Observable;
import java.util.Observer;

/**
 * Created by Administrator on 2017/12/19 0019.
 */

public class RegistActivity extends BaseActivity implements Observer {
    private EditText etPhone,etVertifyCode,etPassword,etConfirmPassword;
    private TextView sendMessageTv,tvRegister,agreementTv;
    private CheckBox cbAgreement;
    private String verifyCode;//验证码
    private BranchAddressPresenter presenter;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_register);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
        presenter=new BranchAddressPresenter(this);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
                //发送验证码
            case R.id.sendMessageTv:
            if (etPhone.getText().toString()!=null){

            if (etPhone.getText().length()>20||etPhone.getText().length()<6){
                MAlert.alert("请输入电5-20位号码");
            }

            presenter.getVerifyCode(etPhone.getText().toString(),1,6,3,120);

            }else {
                MAlert.alert("请先输入您的电话号码");
            }
                break;

                //立即注册
            case R.id.tvRegister:

                if (!cbAgreement.isChecked()){
                    MAlert.alert("同意协议后才能注册");
                }else {

                    if (etVertifyCode.getText().toString()!=null){//验证码
                        if (verifyCode!=null){
                            if (!verifyCode.equals(etVertifyCode.getText().toString())){
                                MAlert.alert("验证码输入错误");
                            }
                        }

                        if (etPassword.getText().toString()!=null){//密码

                            if (etConfirmPassword.getText().toString()!=null&&etPassword
                                    .getText().toString()
                                    .equals(etConfirmPassword.getText().toString())){//确认密码

                                presenter.register("",etPhone.getText().toString()
                                        ,etVertifyCode.getText().toString()
                                        ,etPassword.getText().toString());

                            }else {MAlert.alert("两次密码输入不一致");}
                        }else {MAlert.alert("密码不能为空");}
                    }else {MAlert.alert("验证码不能为空");
                    }

                }


                break;

            case R.id.agreementTv:
                Intent intent = new Intent(this, AgreementActivity.class);
                intent.putExtra("url","http://web1.jinpu.8raw.com/post/user_agreement");
                startActivity(intent);
                break;

        }
    }

    @Override
    public void update(Observable o, Object arg) {
        ResultEntity resultEntity = handlerError(arg);
        if (resultEntity!=null){
            if (resultEntity.getEventType()==BranchAddressPresenter.By_UserAccount_register_success){
                MAlert.alert(resultEntity.getMsg()+"uid=="+resultEntity.getData(), Gravity.TOP);
                Intent intent = new Intent(this, LoginActivity.class);
                intent.putExtra("phone",etPhone.getText().toString());

                startActivity(intent);
                RegistActivity.this.finish();
            }else if (resultEntity.getEventType()==BranchAddressPresenter.By_UserAccount_register_fail){
                MAlert.alert(resultEntity.getMsg());
            }
            if (resultEntity.getEventType()==BranchAddressPresenter.By_SecurityCode_create_success){
               verifyCode= (String) resultEntity.getData();
                etVertifyCode.setText(verifyCode);
            }else if (resultEntity.getEventType()==BranchAddressPresenter.By_SecurityCode_create_fail){
//                MAlert.alert("验证码获取失败");
            }

        }else {

            MAlert.alert("注册失败返回值为空");
        }

    }


}
